﻿using MetaView.Shell.Common;
using MetaView.Shell.Models.Pages;
using MetaView.Shell.Tools.Helper;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json.Linq;
using System.IO;
using System.Windows.Input;
using Wpf.Ui;
using Wpf.Ui.Appearance;
using Wpf.Ui.Controls;

namespace MetaView.Shell.Views.Windows
{
    public partial class MainWindowView
    {
        public MainWindowView()
        {
            SystemThemeWatcher.Watch(this);
           
            //读取文件内容
            string fileContent = File.ReadAllText(GlobalStaticData.AppSettingsFilePath);
            //解析文件内容
            JObject? jsonObject = JObject.Parse(fileContent);

            #region 初始化主题
            string theme = jsonObject["AppSettings"]?["Theme"]?.ToString() ?? "Light";
            switch (theme)
            {
                case "Dark":
                    ApplicationThemeManager.Apply(ApplicationTheme.Dark);
                    break;
                case "System":
                    string systemTheme = SystemHelper.GetSystemTheme();
                    if (systemTheme == "Dark")
                    {
                        ApplicationThemeManager.Apply(ApplicationTheme.Dark);
                    }
                    break;
            }
            #endregion

            #region 初始化语言
            GlobalStaticData.LanguageNum = (int)(jsonObject?["AppSettings"]?["Language"]!);    
            ComMethod.LanguageWinDicHelper(GlobalStaticData.LanguageNum, this);
            #endregion

            InitializeComponent();

            //窗体拖动
            MouseDown += (s, e) =>
            {
                if (e.LeftButton == MouseButtonState.Pressed)
                {
                    DragMove();
                }
            };
        }

        /// <summary>
        /// 窗体加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FluentWindow_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            #region 窗体加载事件

            //读取文件内容
            string fileContent = File.ReadAllText(GlobalStaticData.AppSettingsFilePath);

            //解析文件内容
            JObject? jObject = JObject.Parse(fileContent);

            switch (jObject?["AppSettings"]?["Theme"]!.ToString())
            {
                case "0": ApplicationThemeManager.Apply(ApplicationTheme.Light); break;
                case "1": ApplicationThemeManager.Apply(ApplicationTheme.Dark); break;
            }

            //容器
            //App.Current.Services.GetRequiredService<INavigationService>().SetNavigationControl(NavigationView);
            //App.Current.Services.GetRequiredService<ISnackbarService>().SetSnackbarPresenter(SnackbarPresenter);
            //App.Current.Services.GetRequiredService<IContentDialogService>().SetDialogHost(RootContentDialog);

            //NavigationView.SetServiceProvider(App.Current.Services.GetRequiredService<IServiceProvider>());

            //测试日志
            //LogHelper.Istance.Write("打印测试日志");

            #endregion
        }
    }
}
